/**
 *    Copyright (C) 2009 Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_API_METADATA_NUMBER_HPP
#define _CUBEFS_CORE_API_METADATA_NUMBER_HPP

#include <cubefs/api/exceptions.hpp>
#include <cubefs/api/metadata/number.hpp>
#include <cubefs/core/api/metadata/field.hpp>

#define CUBEFS_SOURCE "cubefs/core/api/metadata/number.hpp:" CUBEFS_AT

namespace cubefs { namespace api {

template <typename Interface>
class NumberFieldImplT: public FieldMetadataImplT<Interface> {
	typedef NumberFieldImplT<Interface> 		Me;
	typedef FieldMetadataImplT<Interface> 		Base;
public:

	NumberFieldImplT(Int ptr, Int abi_ptr, Byte kCode, StringRef name, Int offset, Int item_size, Int count):
		Base(ptr, abi_ptr, kCode, name, offset, offset + count*item_size, item_size) {}

	NumberFieldImplT(Int ptr, Int abi_ptr, Byte kCode, StringRef name, Int item_size, Int count):
		Base(ptr, abi_ptr, kCode, name, 0, count*item_size, item_size) {}

	virtual void GetValue(Page* page, Int idx, Number* number, bool abi);
	virtual void SetValue(Page* page, Int idx, Number* number, bool abi);

	virtual BigInt GetValue(Page* page, Int idx, bool abi) 				= 0;
	virtual void SetValue(Page* page, Int idx, BigInt number, bool abi) = 0;

	static void Init() {
	    AbstractType *type = new AbstractType(TypeNameFactory<Interface>::name(), FieldMetadata::MyType());
	    type_ = type;

	    type->AddOperation(new BasicOperation<void (Me::*)(Page*, Int, Number*, bool)>(type, "GetValue", "page", "idx", "number", "abi", &Me::GetValue));
	    type->AddOperation(new BasicOperation<void (Me::*)(Page*, Int, Number*, bool)>(type, "SetValue", "page", "idx", "number", "abi", &Me::SetValue));

	    type->AddOperation(new BasicOperation<BigInt (Me::*)(Page*, Int, bool)>(type, "GetValue", "page", "idx", "abi", &Me::GetValue));
	    type->AddOperation(new BasicOperation<void (Me::*)(Page*, Int, BigInt, bool)>(type, "SetValue", "page", "idx", "value", "abi", &Me::SetValue));
	}

    CUBEFS_DECLARE_REFLECTION;
};


template <typename Interface>
Type* NumberFieldImplT<Interface>::type_ 					= NULL;

typedef NumberFieldImplT<NumberField> 						NumberFieldImpl;



template <typename Interface>
void NumberFieldImplT<Interface>::GetValue(Page* page, Int idx, Number* number, bool abi) {
    if (number == NULL) {
        throw NullPointerException(CUBEFS_SOURCE, "number must not be NULL");
    }

    if (page == NULL) {
        throw NullPointerException(CUBEFS_SOURCE, "page must not be NULL");
    }

    if (number->IsInstance(Base::valueType_))
    {
        void *ptr0 = abi ? Base::ValuePtrAbi(page->Ptr(), idx) : Base::ValuePtr(page->Ptr(), idx);
        number->set(ptr0);
    }
    else
    {
        throw TypeCastException(CUBEFS_SOURCE, "Number is not an istance of " + Base::valueType_->name(), number->type(), Base::valueType_);
    }
}

template <typename Interface>
void NumberFieldImplT<Interface>::SetValue(Page* page, Int idx, Number* number, bool abi) {
    if (number == NULL) {
        throw NullPointerException(CUBEFS_SOURCE, "number must not be NULL");
    }

    if (page == NULL) {
        throw NullPointerException(CUBEFS_SOURCE, "page must not be NULL");
    }

    if (number->IsInstance(Base::valueType_))
    {
        void *ptr0 = abi ? Base::ValuePtrAbi(page->Ptr(), idx) : Base::ValuePtr(page->Ptr(), idx);
        number->get(ptr0);
    }
    else
    {
        throw TypeCastException(CUBEFS_SOURCE, "Number is not an istance of " + Base::valueType_->name(), number->type(), Base::valueType_);
    }
}




}}

#undef CUBEFS_SOURCE
#endif
